//
//  HYHomeViewController.m
//  Booking
//
//  Created by Duntech on 2018/4/18.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYHomeViewController.h"
#import "HYHomeTableViewCell.h"
#import "HYMeetingRoomBookingVC.h"
#import "HYSeatBookingVC.h"
#import "ScanQRCodeViewController.h"
#import "HYMyBookingVC.h"
#import "HYMeetingScheduleVC.h"
#import "XTPopView.h"
#import "HYAboutUsVC.h"

@interface HYHomeViewController () <SelectIndexPathDelegate>

/** dataSource */
@property (nonatomic,strong) HYTableViewDataSource *dataSource;
/** delegate */
@property (nonatomic,strong) HYTableViewDelegate *delegate;
/** dataSets数据源 */
@property (nonatomic,strong) NSMutableArray *dataSetsArray;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) CGRect frameInNaviView;

@end

@implementation HYHomeViewController

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:KBookSussessNotification];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupData];
    [self setupSubViews];
    [self configcellDelegate];
    KAdjustsScrollViewInsets_NO(self, self.tableView);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpToMyBooking) name:KBookSussessNotification object:nil];
}

- (void)setupNav{
    
    [super setupNav];
    UIBarButtonItem *settingItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting"] style:UIBarButtonItemStylePlain target:self action:@selector(settingAction)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    label.text = @"M Booking";
    label.textColor = KAPP_NAV_TITLE_COLOR;
    label.textAlignment = NSTextAlignmentRight;
    label.adjustsFontSizeToFitWidth = YES;
    UIBarButtonItem *rightImgItem = [[UIBarButtonItem alloc] initWithCustomView:label];
    self.navigationItem.rightBarButtonItems = @[settingItem,rightImgItem];
    
    //导航栏左侧图标
    UIImageView *leftImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo"]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImgView];
}

- (void)setupData{
    
    self.dataSource.dataSource = self.dataSetsArray;
    self.delegate.dataSource = self.dataSetsArray;
    NSMutableArray *sectionArray = [NSMutableArray array];
    NSArray *titleArray = @[NSLocalizedString(@"bookRoom", nil),
                            NSLocalizedString(@"bookSeat", nil),
                            NSLocalizedString(@"qrCode", nil),
                            NSLocalizedString(@"myBookings", nil)];
    NSArray *backgroundImageArray = @[@"BookRoom",@"BookSeat",@"QRCode",@"MyBooking"];
    for (NSInteger i = 0; i < titleArray.count; i++) {
        
        HYHomeTableViewCellModel *cellModel = [HYHomeTableViewCellModel new];
        cellModel.cellHeight = (KSCREEN_HEIGHT - KNavTotal_HEIGHT) / titleArray.count;
        cellModel.cellIdientifier = @"HYHomeTableViewCell";
        cellModel.title = titleArray[i];
        cellModel.backgroundImageName = backgroundImageArray[i];
        [sectionArray addObject:cellModel];
    }
    [self.dataSetsArray addObject:sectionArray];
    [self.tableView reloadData];
}

- (void)setupSubViews{
    
    [self.view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.tableView registerClass:[HYHomeTableViewCell class] forCellReuseIdentifier:@"HYHomeTableViewCell"];
}

- (void)jumpToMyBooking{
    
    [self.navigationController pushViewController:[HYMyBookingVC new] animated:YES];
}

- (void)configcellDelegate{
    
    __weak typeof(self) weakSelf = self;
    [self.delegate setDidSelectedBlock:^(UITableView *tableView, NSIndexPath *indexPath) {
       
        HYBaseCellModel *model = weakSelf.dataSetsArray[indexPath.section][indexPath.row];

        if (indexPath.row == 0) {
            HYMeetingRoomBookingVC *meetingBookingVC = [HYMeetingRoomBookingVC new];
            [weakSelf.navigationController pushViewController:meetingBookingVC animated:YES];
        }
        
        if (indexPath.row == 1) {
            
            HYSeatBookingVC *seatBookingVC = [HYSeatBookingVC new];
            [weakSelf.navigationController pushViewController:seatBookingVC animated:YES];
        }
        
        if (indexPath.row == 2) {
            
            if (TARGET_IPHONE_SIMULATOR) {
                
                [JRToast showWithText:@"Simulator is not support camera" duration:2];
                return;
            }
            
            ScanQRCodeViewController *scanQRCodeVC = [ScanQRCodeViewController new];
//            [weakSelf.navigationController presentViewController:scanQRCodeVC animated:YES completion:nil];
            [weakSelf.navigationController pushViewController:scanQRCodeVC animated:YES];
        }
        
        if (indexPath.row == 3) {
            
            HYMyBookingVC *myBookingVC = [HYMyBookingVC new];
            [weakSelf.navigationController pushViewController:myBookingVC animated:YES];
        }
        
        if ([model.title isEqualToString:@"会议室日程表"]) {
            
            HYMeetingScheduleVC *meetingScheduleVC = [HYMeetingScheduleVC new];
            [weakSelf.navigationController pushViewController:meetingScheduleVC animated:YES];
        }
        
    }];
}

#pragma mark - action
- (void)settingAction{
    
    CGPoint point = CGPointMake(KSCREEN_WIDTH - 30 ,KNavTotal_HEIGHT - 10);
    NSLog(@"%@",NSStringFromCGPoint(point));
    XTPopTableView *popView = [[XTPopTableView alloc] initWithOrigin:point Width:150 Height:50 * 2 Type:XTTypeOfUpRight Color:KAPP_NAV_TITLE_COLOR];
    popView.cornerRadius = 5.0;
    popView.dataArray       = @[NSLocalizedString(@"about", nil), NSLocalizedString(@"logout", nil)];
    popView.row_height      = 50;
    popView.delegate        = self;
    popView.titleTextColor  = KAPP_WHITE_COLOR;
    popView.fontSize        = 15;
    [popView popView];
}

#pragma mark - SelectIndexDelegate

- (void)selectIndexPathRow:(NSInteger)index{
    
    switch (index) {
        case 0:{
         
            HYAboutUsVC *aboutUsVC = [HYAboutUsVC new];
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }
            break;
        case 1:{
            kWeakSelf(weakSelf);
            HYAlertView *alert = [[HYAlertView alloc] initWithTitle:NSLocalizedString(@"tips", nil) message:NSLocalizedString(@"confirmLogoutTips", nil) sureBtn:NSLocalizedString(@"confirm", nil) cancleBtn:NSLocalizedString(@"cancel", nil)];
            alert.resultIndex = ^(NSInteger index) {
              
                [[HYUserModel sharedInstance] logout];
                
                if ([weakSelf respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
                    [weakSelf dismissViewControllerAnimated:YES completion:nil];
                }
                else{
                    KEYWINDOW.rootViewController = [HYLoginViewController new];
                }
            };
            [alert showAlertView];
        }
            break;
        default:
            break;
    }
}

#pragma mark - lazyload
- (HYTableViewDataSource *)dataSource{
    
    if (!_dataSource) {
        _dataSource = [HYTableViewDataSource new];
    }
    return _dataSource;
}

- (NSMutableArray *)dataSetsArray{
    
    if (!_dataSetsArray) {
        _dataSetsArray = [NSMutableArray array];
    }
    return _dataSetsArray;
}

- (HYTableViewDelegate *)delegate{
    
    if (!_delegate) {
        _delegate = [HYTableViewDelegate new];
    }
    return _delegate;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = KAPP_TableView_BgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self.delegate;
        _tableView.dataSource = self.dataSource;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
